package com.gxa.saas.exceptionhandler;




import com.gxa.saas.exception.BusinessException;
import com.gxa.saas.utils.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@ControllerAdvice
public class ExceptionController {

/*
根据异常类型,选择不同的方法,ExceptionHandler的属性value
    @ExceptionHandler(Exception.class)
    public Map exceptionHandler(){

    }*/

    /**
     * 也可以选择讲所有异常集中处理
     * 异常的信息是通过声明参数的形式,让SpringMVC自动注入
     * @return
     */
    @ExceptionHandler(Throwable.class)
    public Map businessException(Exception e){
        JsonResult jsonResult=new JsonResult();
        if (e instanceof BusinessException){
         BusinessException be=((BusinessException)e);
         jsonResult.setCode(be.getExceptionCode().getCode());
         jsonResult.setMsg(be.getExceptionCode().getMessage());
        }else {
            jsonResult.setCode("500");
            jsonResult.setMsg("服务器开小差了,请稍后在试");
            e.printStackTrace();
        }
        return jsonResult.getMap();
    }
}
